function y = xt_mean2d(x, xtflag1, xtval1, xtflag2, xtval2)
% x: vector
% y: panel (xtval1 * xtval2)
% missing = nan
n1 = length(xtval1);
n2 = length(xtval2);

y = nan*ones(n1,n2);
for i=1:1:n1
    temp_x = x(xtflag1==xtval1(i),:);
    temp_f = xtflag2(xtflag1==xtval1(i),:);
    
    for j=1:1:n2
        
        temp_v = temp_x(temp_f==xtval2(j));
        
        if ~isempty(temp_v)
            y(i,j) = mean(temp_v)   ;
        end
        
    end
end